<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <ui-header
        :is-collapse="isCollapse"
        @toggle-collapse="toggleCollapse"
      />
    </el-header>

    <el-container class="main-container">
      <!-- 使用侧边栏组件 -->
      <ui-aside :is-collapse="isCollapse" />

      <!-- 主要内容区域 -->
      <el-main ref="mainContent" @scroll="handleScroll">
        <transition name="fade-transform" mode="out-in">
          <keep-alive>
            <router-view></router-view>
          </keep-alive>
        </transition>

        <!-- 返回顶部按钮 -->
        <el-backtop
          target=".el-main"
          :visibility-height="200"
          :right="40"
          :bottom="40"
        >
          <div class="back-to-top">
            <i class="el-icon-caret-top"></i>
          </div>
        </el-backtop>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import UiAside from '@/components/ui-aside.vue'
import UiHeader from '@/components/ui-header.vue'

export default {
  name: 'HomeView',
  components: {
    UiAside,
    UiHeader
  },
  data () {
    return {
      isCollapse: false,
      showBackToTop: false
    }
  },
  methods: {
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    handleScroll (e) {
      const scrollTop = e.target.scrollTop
      this.showBackToTop = scrollTop > 200
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100%;
  width: 100%;
}

.main-container {
  height: calc(100% - 60px);
}

.el-header {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
  position: relative;
  z-index: 1;
  height: 60px;
  padding: 0;
}

.el-main {
  background-color: #f5f7fa;
  padding: 20px;
  height: 100%;
  overflow-y: auto;
  position: relative;
}

/* 内容区域过渡动画 */
.fade-transform-enter-active,
.fade-transform-leave-active {
  transition: all .5s;
}

.fade-transform-enter {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

/* 返回顶部按钮样式 */
.back-to-top {
  height: 100%;
  width: 100%;
  background-color: #409EFF;
  box-shadow: 0 0 6px rgba(0,0,0, 0.12);
  text-align: center;
  line-height: 40px;
  color: #fff;
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
  transition: all .3s;
}

.back-to-top:hover {
  background-color: #66b1ff;
  transform: translateY(-2px);
}

/* 自定义滚动条 */
.el-main::-webkit-scrollbar {
  width: 6px;
}

.el-main::-webkit-scrollbar-thumb {
  background: #e0e0e0;
  border-radius: 3px;
}

.el-main::-webkit-scrollbar-track {
  background: #f5f5f5;
}
</style>
